<?php
namespace App\Repositories;
use App\Exceptions\Interfaces\UserServiceException;

use App\Interfaces\UserService;
use App\Models\User;
use Log;
use Auth;

class UserRepository implements UserService{


    /**
     *
     * 用户注册
     *
     * @param $user_name
     * @param $password            密码
     * @param $phone
     * @throws
     * @return \App\Models\User|bool
     */
    public function reg($user_name,$password,$phone){

        if ($user_name==''){
            throw UserServiceException('缺少参数:user_name');
        }
        if ($password==''){
            throw UserServiceException('缺少参数:password');
        }
        if ($phone==''){
            throw UserServiceException('缺少参数:phone');
        }

        $user = new User;
        $user->user_name = $user_name;
        $user->password = $password;
        $user->phone = $phone;
        $user->is_disabled = 0;

        $user->save();
        if($user){
            return $user;
        }else{
            throw UserServiceException('注册失败！');
        }
    }

    /**
     *
     * 找回密码
     * @param $phone            手机号
     * @param $newPaw           新密码
     *
     * @throws
     * @return bool
     */

    public function findPaw($phone,$newPaw){

        if ($phone==''){
            throw UserServiceException('缺少参数:phone');
        }
        if ($newPaw==''){
            throw UserServiceException('缺少参数:newPaw');
        }
        $user = User::where('phone',$phone)->update(['password'=>md5($newPaw)]);
        // dd(User::where('password',md5($newPaw))->get());
        return $user;


    }

    /**
     *
     * 修改用户密码
     *
     * @param $user_name          用户名
     * @param $oldPassword
     * @param $newPassword
     * @throws
     * @return mixed
     */

    public function updatePaw($user_name,$oldPassword,$newPassword){
        if ($user_name==''){
            throw new UserServiceException('缺少参数:user_name');
        }
        if ($oldPassword==''){
            throw new UserServiceException('缺少参数:oldPassword');
        }
        if ($newPassword==''){
            throw new UserServiceException('缺少参数:newPassword');
        }

        $is_exit = User::where('user_name', $user_name)->where('Password', md5($oldPassword))->get();
        //dd("daf");
        if($is_exit){
            $user = User::where('user_name', $user_name)->where('Password', md5($oldPassword))->update(['password'=>md5($newPassword)]);
            return $user;

        }else{

            throw UserServiceException("该用户不存在！");
            //return false;
        }


    }

    /**
     * 获取我的信息
     * @param $user_name     用户名
     * @throws UserServiceException
     * @return mixed
     */
    public function  getInfo($user_name)
    {
        if ($user_name==''){
            throw new UserServiceException('缺少参数:user_name');
        }

        $user = User::where('user_name',$user_name)->get();

        return $user;
    }

    /**
     * 根据用户id获取用户名
     * @param $user_id     用户id
     * @return mixed
     */
    public  function  getUser($user_id){

        $user = User::findOrNew($user_id);

        return $user;
    }



    /**
     * 根据用户id查询用户是否实名认证
     * @param [number] $user_id [用户id]
     */
    public function hasIdVerity($user_id){

        $user = User::findOrNew($user_id);

        if(!$user || empty($user->real_name) || empty($user->id_num)){

            return 'false';
        }

        return 'true';
    }


    /**
     * 增加用户实名信息
     * @param [number] $user_id [用户id]
     * @param [string] $real_name [用户真实姓名]
     * @param [number] $id_type   [证件类型，1：身份证]
     * @param [string] $id_num    [证件号]
     */
    public function addIdVerity($user_id, $real_name, $id_type, $id_num){

        Log::info('增加用户注册信息');

        try {

            // $user = User::find($user_id);
            $user = User::where('id', $user_id);

        } catch (UserServiceException $e) {

            throw UserServiceException('用户信息不存在');
        }

        try {

            $user->update(['real_name'=>$real_name, 'id_type'=>$id_type, 'id_num'=>$id_num]);

        } catch (UserServiceException $e) {

            throw UserServiceException('增加用户实名信息失败');
        }

        return $user;
    }

    /**
     * 获取当前的用户对象
     * @return [type] [description]
     */
    public function getAuth()
    {

        return Auth::user();
    }
}